"""
https://leetcode.cn/problems/reshape-data-pivot/description/?envType=study-plan-v2&envId=introduction-to-pandas&lang=pythondata

2889. 数据重塑：透视
简单
premium lock icon
相关企业
提示
DataFrame weather
+-------------+--------+
| Column Name | Type   |
+-------------+--------+
| city        | object |
| month       | object |
| temperature | int    |
+-------------+--------+
编写一个解决方案，以便将数据 旋转，使得每一行代表特定月份的温度，而每个城市都是一个单独的列。

输出结果格式如下示例所示。

 

示例 1:
输入：
+--------------+----------+-------------+
| city         | month    | temperature |
+--------------+----------+-------------+
| Jacksonville | January  | 13          |
| Jacksonville | February | 23          |
| Jacksonville | March    | 38          |
| Jacksonville | April    | 5           |
| Jacksonville | May      | 34          |
| ElPaso       | January  | 20          |
| ElPaso       | February | 6           |
| ElPaso       | March    | 26          |
| ElPaso       | April    | 2           |
| ElPaso       | May      | 43          |
+--------------+----------+-------------+
输出：
+----------+--------+--------------+
| month    | ElPaso | Jacksonville |
+----------+--------+--------------+
| April    | 2      | 5            |
| February | 6      | 23           |
| January  | 20     | 13           |
| March    | 26     | 38           |
| May      | 43     | 34           |
+----------+--------+--------------+
解释：
表格被旋转，每一列代表一个城市，每一行代表特定的月份。

"""

import pandas as pd

def pivotTable(weather: pd.DataFrame) -> pd.DataFrame:
    # 先转列
    result=weather.pivot(index='month',columns='city',values='temperature')

    return result

if __name__ == '__main__':
    weather = pd.DataFrame({
        'city': ['Jacksonville', 'Jacksonville', 'Jacksonville', 'Jacksonville', 'Jacksonville',
                 'ElPaso', 'ElPaso', 'ElPaso', 'ElPaso', 'ElPaso'],
        'month': ['January', 'February', 'March', 'April', 'May',
                  'January', 'February', 'March', 'April', 'May'],
        'temperature': [13, 23, 38, 5, 34, 20, 6, 26, 2, 43]
    })
    res=pivotTable(weather)
    print(res)